//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pt;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.MemberInfoDao;
import com.aidong.dao.wills.PtChangeCoachDao;
import com.aidong.dao.wills.PtCoachCodeDao;
import com.aidong.dao.wills.PtContractDao;
import com.aidong.model.wills.PtChangeCoach;
import com.aidong.model.wills.PtContract;
import com.aidong.web.controller.member.PayController;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"ptChangeCoach"})
public class PtChangeCoachController extends PayController {
    @Resource
    PtChangeCoachDao ptChangeCoachDao;
    @Resource
    PtCoachCodeDao ptCoachCodeDao;
    @Resource
    PtContractDao ptContractDao;
    @Resource
    MemberInfoDao memberInfoDao;

    public PtChangeCoachController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return new ModelAndView("pt/changecoach/list");
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "cotractCode",required = false) String cotractCode, @RequestParam(value = "memberCode",required = false) String memberCode, @RequestParam(value = "coachCode",required = false) String coachCode, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "b.shopCode", shopCode);
        cd.eq("b.cotractCode", cotractCode);
        cd.eq("b.memberCode", memberCode);
        if(!this.chkNull(coachCode)) {
            ConditionMap mv = new ConditionMap(1);
            mv.eq("b.coachCode", coachCode);
            mv.eq("b.newCoachCode", coachCode);
            cd.addSub(mv);
        }

        if(!this.chkNull(dateFrom)) {
            cd.ge("b.createTime", this.parseDate(dateFrom, "yyyy-MM-dd"));
        }

        if(!this.chkNull(dateTo)) {
            cd.le("b.createTime", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }

        this.ptChangeCoachDao.queryPageList(pg, cd);
        ModelAndView mv1 = new ModelAndView("pt/changecoach/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        List coachList = this.ptCoachCodeDao.getShopCoachList(this.getLoginShopCode());
        ModelAndView mv = new ModelAndView("pt/changecoach/add");
        mv.addObject("coachList", coachList);
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("changeCoach") PtChangeCoach changeCoach) {
        if(this.chkNull(changeCoach.getContractCode())) {
            return this.getResult(false, "请输入合同号");
        } else if(this.chkNull(changeCoach.getNewCoachCode())) {
            return this.getResult(false, "请选择新教练");
        } else {
            PtContract contract = this.ptContractDao.getByContractCode(changeCoach.getContractCode());
            if(contract == null) {
                return this.getResult(false, "私教合同号错误!");
            } else if(contract.getStatus().intValue() == 1 && contract.getOutTime() == null) {
                if(contract.getBespeakQuantity().intValue() >= contract.getQuantity().intValue()) {
                    return this.getResult(false, "合同已经没有课时");
                } else {
                    changeCoach.setShopCode(this.getLoginShopCode());
                    changeCoach.setContractCode(contract.getContractCode());
                    changeCoach.setMemberCode(contract.getMemberCode());
                    changeCoach.setPtCode(contract.getPtCode());
                    changeCoach.setCoachCode(contract.getCoachCode());
                    changeCoach.setCreateUser(this.getLoginUserCode());
                    changeCoach.setCreateTime(new Date());
                    changeCoach.setUpdateTime(new Date());

                    try {
                        this.ptChangeCoachDao.addChangeCoach(changeCoach);
                        this.addUserLog("私教换教练", "合同号:" + changeCoach.getContractCode() + ",会员:" + changeCoach.getMemberCode());
                    } catch (Exception var4) {
                        this.log.error("add changeCoach", var4);
                        return this.getResult(false, "保存发生错误");
                    }

                    return this.getResult(true, "保存完成");
                }
            } else {
                return this.getResult(false, "无效合同");
            }
        }
    }
}
